﻿using System;
using System.Net;
using System.Runtime.Serialization;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Linq;
using JustinAngel.Neurons.Pages.Main;

namespace JustinAngel.Neurons.ServerModel
{
    [DataContract]
    public class RssItem
    {
        [DataMember(Name = "id")]
        public Uri YouTubeLink { get; set; }

        [DataMember(Name = "neuron")]
        public string NeuronIdentifier { get; set; }

        public bool IsRSA { get { return NeuronIdentifier == "RSA"; } }
        public bool IsTED { get { return NeuronIdentifier == "TED"; } }
        public bool IsScienceDump { get { return NeuronIdentifier == "ScienceDump"; } }
        public bool IsForaTV { get { return NeuronIdentifier == "FORA"; } }

        [DataMember(Name = "link")]
        public Uri ExternalLink { get; set; }

        [DataMember(Name = "title")]
        public string Title { get; set; }

        [DataMember(Name = "published")]
        public DateTime PublicationDate { get; set; }

        [DataMember(Name = "thumbnail")]
        public Uri ScienceDumpThumbnail { get; set; }

        [DataMember(Name = "media:group")]
        public MediaGroupRssItem MediaGroup { get; set; }

        public Uri Thumbnail
        {
            get
            {
                if (IsScienceDump)
                    return ScienceDumpThumbnail;
                else
                {
                    var thumb = MediaGroup.MediaThumbnails.FirstOrDefault(t => t.Width == 120);
                    if (thumb == null)
                        return null;
                    else
                        return thumb.Url;
                }
            }
        }

        public Uri FinalYouTubeLink
        {
            get
            {
                if (IsScienceDump)
                    return ExternalLink;
                else
                    return YouTubeLink;
            }
        }

        public string GroupDisplayName
        {
            get
            {
                if (IsTED) return "TED Talks";
                if (IsRSA) return "The RSA";
                if (IsForaTV) return "FORA.tv";
                if (IsScienceDump) return "Science Dump";
                return String.Empty;
            }
        }

        public VideoItemViewModel ToVideoItemViewModel
        {
            get
            {
                return new VideoItemViewModel(Thumbnail, FinalYouTubeLink, ExternalLink, Title, GroupDisplayName, HighResImageUrl);
            }
        }

        protected Uri HighResImageUrl
        {
            get
            {
                if (IsScienceDump)
                    return ScienceDumpThumbnail;
                else
                {
                    var thumb = MediaGroup.MediaThumbnails.FirstOrDefault(t => t.Width == 320);
                    if (thumb == null)
                        return null;
                    else
                        return thumb.Url;
                }
            }
        }
    }
}
